package org.zch.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.zch.common.ApiResult;
import org.zch.entity.dto.WorksDTO;
import org.zch.entity.dto.WorksUserDTO;
import org.zch.entity.po.RecommendPO;
import org.zch.entity.po.WorksPO;
import org.zch.service.RecommendService;
import org.zch.service.WorksService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 作品表(Works)表控制层
 *
 * @author 刘新
 * @since 2024-01-03 14:09:05
 */
@RestController
@RequestMapping("works")
@Api(tags = "作品")
public class WorksController {
    /**
     * 作品service
     */
    @Resource
    private WorksService worksService;

    /**
     * 作品推荐service
     */
    @Resource
    private RecommendService recommendService;

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param worksPO 查询实体
     * @return 所有数据
     */
    @PostMapping("/getPage")
    public ApiResult selectAll(@RequestBody Page<WorksPO> page, @RequestBody WorksPO worksPO) {
        return ApiResult.ok(this.worksService.page(page, new QueryWrapper<>(worksPO)));
    }

    /**
     * 新增数据（发布作品）
     *
     * @param worksPO 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "发布作品", notes = "发布作品")
    @PostMapping("/insert")
    public ApiResult insert(@RequestBody WorksPO worksPO) {
        return ApiResult.ok(this.worksService.insert(worksPO));
    }


    /**
     * 热门列表
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @ApiOperation(value = "热门列表", notes = "热门列表")
    @PostMapping("/hotList")
    public ApiResult hotList(@RequestBody WorksDTO page) {
        return ApiResult.ok(this.worksService.hotList(page));
    }


    /**
     * 最新列表
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @ApiOperation(value = "最新列表", notes = "最新列表")
    @PostMapping("/newList")
    public ApiResult newList(@RequestBody WorksDTO page) {
        return ApiResult.ok(this.worksService.newList(page));
    }

    /**
     * 发现列表(只查询有图片、音频的数据)
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @ApiOperation(value = "发现列表", notes = "发现列表")
    @PostMapping("/foundList")
    public ApiResult foundList(@RequestBody WorksDTO page) {
        return ApiResult.ok(this.worksService.foundList(page));
    }

    /**
     * 更新接口
     *
     * @param updateWrapper 分页对象
     * @return 所有数据
     */
    @ApiOperation(value = "更新接口", notes = "更新接口")
    @PostMapping("/update")
    public ApiResult update(@RequestBody WorksPO updateWrapper) {
        return ApiResult.ok(this.worksService.updateById(updateWrapper));
    }

    /**
     * 根据id 删除作品
     */
    @ApiOperation(value = "删除作品", notes = "删除作品")
    @PostMapping("/deleteWork")
    public ApiResult deleteWork(@RequestParam("id") Integer id) {
        return ApiResult.ok(this.worksService.deleteWork(id));
    }


    /**
     * 推荐列表
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @ApiOperation(value = "推荐列表", notes = "推荐列表")
    @PostMapping("/recommendList")
    public ApiResult recommendList(@RequestBody WorksDTO page) {
        return ApiResult.ok(this.recommendService.recommendList(page));
    }


    /**
     * 管理员用-待审核列表
     */
    @ApiOperation(value = "待审核列表", notes = "待审核列表")
    @PostMapping("/awaitList")
    public ApiResult awaitList(@RequestBody Page<WorksPO> page) {
        return ApiResult.ok(this.worksService.awaitList(page));
    }


    /**
     * 根据userCode获取用户作品数量
     *
     * @param userCode 用户编码
     * @return
     */
    @ApiOperation(value = "根据userId获取用户作品数量", notes = "根据userId获取用户作品数量")
    @PostMapping("/getWorksQuantityByUserCode")
    public ApiResult getWorksQuantityByUserCode(@RequestParam("userCode") String userCode) {
        return ApiResult.ok(this.worksService.getWorksQuantityByUserCode(userCode));
    }

    /**
     * 根据code、状态获取用户作品
     *
     * @param page 查询对象
     * @return 返回结果
     */
    @ApiOperation(value = "根据code、状态获取用户作品", notes = "根据code、状态获取用户作品")
    @PostMapping("/getWorksByUserCode")
    public ApiResult getWorksByUserCode(@RequestBody WorksUserDTO page) {
        return ApiResult.ok(this.worksService.getWorksByUserCode(page));
    }


    /**
     * 批量更新是否看过字段
     *
     * @param map ids:作品ids
     *            userCode:用户编码
     * @return 更新的条数
     */
    @ApiOperation(value = "批量更新是否看过字段", notes = "批量更新是否看过字段")
    @PostMapping("/updateLooked")
    public ApiResult updateLooked(@RequestBody Map<String, Object> map) {
        return ApiResult.ok(this.worksService.updateLooked(map));
    }

    /**
     * 将作品设为推荐
     *
     * @return 更新的条数
     */
    @ApiOperation(value = "将作品设为推荐", notes = "将作品设为推荐")
    @PostMapping("/recommendWorks")
    public ApiResult recommendWorks(@RequestBody List<RecommendPO> list) {
        return ApiResult.ok(this.recommendService.recommendWorks(list));
    }

    /**
     * 点赞作品
     *
     * @return 更新的条数
     */
    @ApiOperation(value = "点赞作品", notes = "点赞作品")
    @PostMapping("/likeWorks")
    public ApiResult likeWorks(@RequestBody WorksPO worksPO) {
        return ApiResult.ok(this.worksService.likeWorks(worksPO));
    }

    /**
     * 取消点赞
     *
     * @return 更新的条数
     */
    @ApiOperation(value = "取消点赞", notes = "取消点赞")
    @PostMapping("/noLikeWorks")
    public ApiResult noLikeWorks(@RequestBody WorksPO worksPO) {
        return ApiResult.ok(this.worksService.noLikeWorks(worksPO));
    }


    /**
     * 点赞列表
     *
     * @return 分页列表
     */
    @ApiOperation(value = "点赞列表", notes = "点赞列表")
    @PostMapping("/likeList")
    public ApiResult likeList(@RequestBody WorksDTO dto) {
        return ApiResult.ok(this.worksService.likeList(dto));
    }

    /**
     * 播放作品的音频
     *
     * @return 更新的条数
     */
    @ApiOperation(value = "播放作品", notes = "播放作品")
    @PostMapping("/playWorks")
    public ApiResult playWorks(@RequestBody WorksPO worksPO) {
        return ApiResult.ok(this.worksService.playWorks(worksPO));
    }


    /**
     * 根据id查询作品
     *
     * @param id 作品id
     * @return
     */
    @ApiOperation(value = "根据id查询作品", notes = "根据id查询作品")
    @PostMapping("/getById")
    public ApiResult getById(@RequestParam("id") Integer id) {
        return ApiResult.ok(this.worksService.getById(id));
    }


    /**
     * 将已经看过的作品添加到过滤器中
     *
     * @param map
     * {
     *     "userCode": "用户号",
     *     "ids": ["作品id1","作品id2"]
     * }
     * @return
     */
    @PostMapping("/addFilter")
    public ApiResult addFilter(@RequestBody Map<String, Object> map) {
        String userCode = map.get("userCode").toString();
        List<Integer> ids = (List<Integer>) map.get("ids");
        return ApiResult.ok(this.worksService.addFilter(userCode, ids));
    }


    /**
     * 随机更新作品发布时间
     *
     * @return
     */
    @GetMapping("/randomUpdateWorkTime")
    public ApiResult randomUpdateWorkTime() {
        this.worksService.randomUpdateWorkTime();
        return ApiResult.ok();
    }



}

